//
//  DDAlertView.m
//  DDMusic
//
//  Created by rimi on 16/11/8.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDAlertView.h"
#import "UILabel+DDUILabelCategory.h"
#import "DDPublicMacro.h"
@interface DDAlertView()
@property (nonatomic,strong) UILabel* alertLabel;
@property (nonatomic,strong) NSTimer* timer;
@property (nonatomic,assign) BOOL alertIsShowing;//记录提示框是否正在显示
@end
@implementation DDAlertView
#pragma mark常用方法
- (void)showAlertViewWithTitle:(NSString*)title{
    if (self.alertIsShowing == NO) {
        //提示label
        self.alertLabel = [UILabel dd_CreatLabelWithTitle:title font:15 TitleColor:[UIColor whiteColor] TextAlignment:(NSTextAlignmentCenter)];
        self.alertLabel.frame = CGRectMake((SCREEN_SIZE.width-(SCREEN_SIZE.width/2))/2, (SCREEN_SIZE.height-40)/2, SCREEN_SIZE.width/2, 40);
        self.alertLabel.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
        self.alertLabel.layer.cornerRadius = 7;
        self.alertLabel.clipsToBounds = YES;
        [[UIApplication sharedApplication].keyWindow addSubview:self.alertLabel];
        self.alertIsShowing = YES;
        //添加动画
        CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
        animation.duration = 0.5;
        NSMutableArray* values = [NSMutableArray array];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
        animation.values = values;
        [self.alertLabel.layer addAnimation:animation forKey:nil];
        //timer
        self.timer =  [NSTimer timerWithTimeInterval:0.8 target:self selector:@selector(methodTime) userInfo:nil repeats:YES];
        if (self.timer!=nil) {
            [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
            
        }

    }
}
- (void)methodTime{
    if (self.timer!=nil) {
        [self.timer invalidate];
        self.timer = nil;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.7];
    [UIView setAnimationDelegate:self];
    self.alertLabel.alpha = 0.0;
    [UIView commitAnimations];
    [self.alertLabel removeFromSuperview];
    self.alertIsShowing = NO;
}

#pragma mark  单例初始化方法
static DDAlertView* instance = nil;
+ (instancetype)sharedDDAlertView{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DDAlertView alloc]init];
    });
    return instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    @synchronized (self) {
        if (!instance){
            instance = [super allocWithZone:zone];
        }
    }
    return instance;
    
}

@end
